﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess.Classes;
using System.IO;

namespace Webmau.Controls
{
    public partial class ThemBaiViet : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        
        protected void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                BaiViet data = new BaiViet();
                
                data.TenBaiViet = txtTenBaiViet.Text;
                data.LoaiTin = txtLoaiTin.SelectedValue.ToString();
                data.NoiDung = txtNoiDung.Text;
                data.ThoiGian = Convert.ToDateTime(DateTime.Now.ToShortDateString().ToString());
                data.Hinh= FileUpload1.FileName.ToString();
                data.IDNguoiDung = Session["id"].ToString();
                BaiViet.Them(data);
                lbThongBao.Text = "Thêm thành công!";
            }
            catch
            {
                lbThongBao.Text = "Thêm thất bại!";
            }
        }
        bool CheckFileType(string fileName)
        {
            string ext = Path.GetExtension(fileName);
            switch (ext.ToLower())
            {
                case ".gif":
                    return true;
                case ".png":
                    return true;
                case ".jpg":
                    return true;
                case ".jpeg":
                    return true;
                case ".bmp":
                    return true;
                default:
                    return false;
            }
        }
        protected void FileUpload1_Init(object sender, EventArgs e)
        {
            if (CheckFileType(FileUpload1.FileName) == true)
            {
                String filePath = "~/HinhBaiViet/" + FileUpload1.FileName;
                FileUpload1.SaveAs(MapPath(filePath));
            }
        }
    }
}